<template>
  <div :class="[className,'echarts']"></div>
</template>

<script>
import {watch, onMounted} from 'vue'
import * as echarts from 'echarts'
import {v4 as uuidv4} from 'uuid'

export default {
  name: "VueEcharts",
  props: {
    option: Object,
    theme: [String, Object]
  },
  setup(ctx) {
    let dom
    let chart
    let className = `echarts${uuidv4()}`
    const initChart = () => {
      if (!chart) {
        dom = document.getElementsByClassName(className)[0]
        chart = echarts.init(dom, ctx.theme)
      }
      if (ctx.option) {
        chart.setOption(ctx.option)
      }
    }
    onMounted(() => {
      initChart()
    })

    watch(() => ctx.option, () => {
      initChart()
    })

    return {
      className
    }
  }
}
</script>

<style lang="scss" scoped>
.echarts {
  width: 100%;
  height: 100%;
}
</style>
